package com.aaa.operations.controller;


import com.aaa.common.entity.*;
import com.aaa.operations.service.RoomService;
import com.aaa.operations.service.RoomUserService;
import com.aaa.operations.service.UserService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  寝室控制层
 * </p>
 *
 * @author ${author}
 * @since 2020-07-29
 */
@RestController
@RequestMapping("/room")
public class RoomController {
    @Autowired
    private RoomService roomService;

    @Autowired
    private UserService userService;

    @Autowired
    private RoomUserService roomUserService;

    /**
     * 查询所有寝室
     * @return
     */
    @ResponseBody
    @RequestMapping("/getAllRoom")
    public ResultData getAllRoom(Integer pageSize,Integer pageNum,String userName,String roomStorey){
        Map map = new HashMap();
        if(userName!=null && !userName.equals("")){
            map.put("userName",userName);
        }if(roomStorey!=null && !roomStorey.equals("")){
            map.put("roomStorey",roomStorey);
        }
        PageInfo pageInfo = roomService.getAllRoom(pageSize,pageNum,map);

        if(pageInfo!=null){
            return new ResultData(true,"查询成功",pageInfo);
        }else {
            return new ResultData(false,"查询失败",null);
        }
    }




    @RequestMapping("/addRoom")
    public ResultData addRoom(@RequestBody Room room){

        Wrapper wrapper=new EntityWrapper();
        wrapper.eq("room_storey",room.getRoomStorey());
        wrapper.eq("room_no",room.getRoomNo());
        Object o = roomService.selectObj(wrapper);
        if(o!=null){
            return new ResultData(false, "新增失败，已有此寝室", null);
        }else {
            boolean insert = roomService.insert(room);
            if (insert) {
                return new ResultData(true, "新增成功", null);
            } else {
                return new ResultData(false, "新增失败", null);
            }
        }
    }


    @RequestMapping("/deleteRoom")
    public ResultData deleteRoom(Integer roomId){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("room_id",roomId);

        boolean delete = roomService.deleteById(roomId);
        if(delete){
            return new ResultData(true,"删除成功",null);
        }else {
            return new ResultData(false,"删除失败",null);
        }
    }

    @RequestMapping("/getUser")
    public ResultData getUser(){
        List<RoomUser> roomUserS = roomUserService.selectList(null);
        Wrapper wrapper = new EntityWrapper();
        for (RoomUser roomUser : roomUserS) {
            wrapper.notIn("user_id",roomUser.getUserId());
        }
        /*wrapper.notIn("user_id",1);
        wrapper.notIn("user_id",3);*/
        List<User> list = userService.selectList(wrapper);

        if(list!=null){
            return new ResultData(true,"查询成功",list);
        }else {
            return new ResultData(false,"查询失败",null);
        }
    }
    @RequestMapping("/getAllStorey")
    public ResultData getAllStorey(){
        List<Room> allStorey = roomService.getAllStorey();
        System.out.println(allStorey);
        if(allStorey!=null){
            return new ResultData(true,"查询成功",allStorey);
        }else {
            return new ResultData(false,"查询失败",null);
        }
    }
    @RequestMapping("/getAllNo")
    public ResultData getAllNo(@RequestParam("roomStorey") Integer roomStorey){
        List<Room> allStorey = roomService.getAllNo(roomStorey);
        if(allStorey!=null){
            return new ResultData(true,"查询成功",allStorey);
        }else {
            return new ResultData(false,"查询失败",null);
        }
    }
    @RequestMapping("/getAllInRoom")
    public ResultData getAllInRoom(){

        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("room_state",1);
        List<Room> list = roomService.selectList(wrapper);
        if(list!=null){
            return new ResultData(true,"查询成功",list);
        }else {
            return new ResultData(false,"查询失败",null);
        }
    }
}

